using System.Net;
using System.Net.Http;
using System.Web;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        List<string> titleResult = new List<string>();
        List<string> bodyResult = new List<string>();
        string result = string.Empty;
        public string search(string keyWord)
        {
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
            string encodedKeyword = HttpUtility.UrlEncode(keyWord, Encoding.GetEncoding(936));
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.baidu.com/s?wd=" + keyWord);
            myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0";
            myHttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            Stream streamResponse = myHttpWebResponse.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);

            HttpWebRequest myHttpWebRequestPage2 = (HttpWebRequest)WebRequest.Create("https://www.baidu.com/s?wd=" + keyWord + "&pn=10");
            myHttpWebRequestPage2.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36";
            myHttpWebRequestPage2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            HttpWebResponse myHttpWebResponsePage2 = (HttpWebResponse)myHttpWebRequestPage2.GetResponse();
            Stream streamResponsePage2 = myHttpWebResponsePage2.GetResponseStream();
            StreamReader streamReadPage2 = new StreamReader(streamResponsePage2);
            
            string responseFromServer = string.Concat(streamRead.ReadToEnd(), streamReadPage2.ReadToEnd());

            streamResponse.Close();
            streamRead.Close();
            myHttpWebResponse.Close();
            return responseFromServer;
        }
        public void titleFilter(string responseFromServer)
        {
            // 使用正则表达式将搜索结果的title找出来
            string strTitle = @"\<h3[\s\S]*?\<\s?a[\s\S]*?\>([\s\S]*?)\</a\>"; // 注意要使用lazy匹配
            MatchCollection matches = new Regex(strTitle).Matches(responseFromServer);
            int i = 1;
            string str = string.Empty;
            foreach (Match match in matches)
            {

                string trim1 = match.Groups[1].Value.Replace(@"<em>", "");
                string trim2 = trim1.Replace(@"</em>", "");
                string trim3 = trim2.Replace(" ", "");
                string trim4 = trim3.Replace("\r\n", "");
                titleResult.Append(trim4);
                str += i.ToString() + ". " + trim4 + "\r\n";
                i++;
            }
            if (str.Length >= 200)
                textBox2.Text += str.Substring(0, 200);
        }
        public void bodyFilter(string responseFromServer)
        {
            string strBody = @"s40""\>([\s\S]*?)\</s";
            MatchCollection matches = new Regex(strBody).Matches(responseFromServer);
            int i = 1;
            string str = string.Empty;
            foreach (Match match in matches)
            {
                string trim1 = match.Groups[1].Value.Replace(@"<em>", "");
                string trim2 = trim1.Replace(@"</em>", "");
                string trim3 = trim2.Replace(" ", "");
                string trim4 = trim3.Replace("\r\n", "");
                bodyResult.Append(trim4);
                str += i.ToString() + ". " + trim4 + "\r\n";
                i++;
            }
            if (str.Length >= 200)
                textBox3.Text += str.Substring(0, 200);
        }
        public void merge()
        {
            for (int i = 0; i < titleResult.Count; i++)
            {
                result += (i + 1).ToString() + ". " + titleResult[i] + "\r\n";
                result += "    " + bodyResult[i] + "\r\n";
            }
        }
        public void printTextbox2()
        {
            if (result.Length >= 200)
            { textBox2.Text = result.Substring(0, 200); }
        }
        public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string responseFromServer = search(textBox1.Text);

            // 并行编程
            Parallel.Invoke(new Action[]
            {
                ()=>titleFilter(responseFromServer),
                ()=>bodyFilter(responseFromServer)
            });
        }
    }
}